package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yx.mapper.UserMapper;
import com.yx.pojo.Emp;
import com.yx.pojo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;

@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //1、查询用户信息
        //创建查询条件
        LambdaQueryWrapper<Emp> empLambdaQueryWrapper = new LambdaQueryWrapper<>();
        empLambdaQueryWrapper.eq(Emp::getUsername,username);
        //查询
        Emp emp = userMapper.selectOne(empLambdaQueryWrapper);
        //如果没有查到抛出异常
        //是 Spring Framework 提供的断言工具类方法，用于快速验证对象非空，如果为空则抛出 IllegalArgumentException
        Assert.notNull(emp,"用户名错误");

        //查询对应的权限信息
        ArrayList<String> list = new ArrayList<>();
        list.add("test");
        // 将用户信息封装到UserDetails实现类中
        return new LoginUser(emp,list);
    }

}
